﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DNA.Mvc.jQuery;

namespace DNA.Mvc.UI
{
    public class FileNodeProvider : HierarchicalNodeProviderBase
    {
        string _root;
        HierarchicalNode rootNode;

        public Func<string, string> GetImageUrl { get; set; }

        public FileNodeProvider(string path)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");
            _root = path;
        }

        public override HierarchicalNode FindNodeFormKey(string key)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<HierarchicalNode> GetChildNodes(HierarchicalNode node)
        {
            string path = node.Item as string;

            var isFile = Path.HasExtension(path);
            if (isFile)
                return null;
            else
            {
                var dirs = from d in Directory.GetDirectories(path)
                           select new HierarchicalNode(this)
                           {
                               Title = new DirectoryInfo(d).Name,
                               ImageUrl = GetImageUrl != null ? GetImageUrl(d) : "",
                               Item = d
                           };
                var files = from f in Directory.GetFiles(path)
                            select new HierarchicalNode(this)
                            {
                                Title = Path.GetFileName(f),
                                ImageUrl = GetImageUrl != null ? GetImageUrl(f) : "",
                                Key=f,
                                Item = f
                            };
                return dirs.Concat(files);
            }

        }

        public override HierarchicalNode GetParentNode(HierarchicalNode node)
        {
            string path = node.Item as string;
            if (path.Equals(_root, StringComparison.OrdinalIgnoreCase))
                return rootNode;

            var isFile = Path.HasExtension(path);
            DirectoryInfo parentInfo;
            if (isFile)
                parentInfo = new DirectoryInfo(Path.GetDirectoryName(path));
            else
                parentInfo = new DirectoryInfo(path).Parent;

            return new HierarchicalNode(this)
            {
                Title = parentInfo.Name,
                Item = parentInfo.FullName,
                ImageUrl = GetImageUrl != null ? GetImageUrl(path) : "",
            };
        }

        public override HierarchicalNode RootNode
        {
            get
            {
                if (rootNode == null)
                {
                    var dir = new DirectoryInfo(_root);
                    rootNode = new HierarchicalNode(this)
                    {
                        Title = dir.Name,
                        Item = dir.FullName,
                        ImageUrl = GetImageUrl != null ? GetImageUrl(_root) : "",
                    };
                }
                return rootNode;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
